<template>
    <div class="converter-select-container" v-if="!unavailable">
        <tnxel-icon class="text-placeholder mx-1" value="Loading" title="依赖库加载中" style="margin-top: 1px;"
            v-if="app.dependencyInitializing"/>
        <tnxel-icon class="text-placeholder mx-1" value="Loading" title="转换器类加载中" style="margin-top: 1px;"
            v-else-if="!items"/>
        <el-badge type="info" is-dot v-else-if="fixedAuto">
            <tnxel-icon class="text-placeholder" value="Switch" :title="items[0].text"/>
        </el-badge>
        <el-dropdown trigger="click" @command="onDropdownCommand" v-else-if="items?.length">
            <el-badge type="primary" is-dot v-if="model">
                <tnxel-icon class="link" value="Switch" :title="'转换器：' + model"/>
            </el-badge>
            <tnxel-icon class="link" value="Switch" :title="items[0].text" v-else/>
            <template #dropdown>
                <el-dropdown-menu>
                    <el-dropdown-item v-for="item of items" :key="item.value" :command="item.value">
                        <tnxel-icon :class="{'text-primary': model === item.value}"
                            :value="model === item.value ? 'Select' : 'fa'"/>
                        <span :class="{'text-primary': model === item.value}">{{ item.text }}</span>
                    </el-dropdown-item>
                </el-dropdown-menu>
            </template>
        </el-dropdown>
    </div>
</template>

<script>
import tnx from '@/tnx.js';
import TypeKind from '@/logic/modeling/enums/TypeKind.js';
import ModelingApp from '@/logic/modeling/entity/ModelingApp.js';
import EntityPropertyMapping from '@/logic/modeling/entity/EntityPropertyMapping.js';
import truenewxLib from '@/logic/modeling/util/dependency-lib-truenewx.js';
import MavenProjectUtil from '@/logic/maven/util/project.js';

export default {
    name: 'ConverterSelect',
    props: {
        modelValue: {
            type: String,
            default: '',
        },
        app: {
            type: ModelingApp,
            required: true,
        },
        property: {
            type: Object,
            required: true,
        },
    },
    data() {
        return {
            TypeKind,
            model: this.modelValue,
            converterClasses: null,
        };
    },
    computed: {
        unavailable() {
            // 可关联类型不可设置转换器，其应该进行关系映射
            return this.property.refKind?.supports('association');
        },
        propertyType() {
            let propertyType = this.property.typeId;
            if (this.property.arrayed) {
                propertyType += '[]';
            }
            return propertyType;
        },
        fixedAuto() {
            return TypeKind.ENUM.equals(this.property.refKind);
        },
        items() {
            if (this.fixedAuto) {
                return [{
                    value: EntityPropertyMapping.CONVERTER_AUTO,
                    text: '自动生成枚举类型转换器',
                }];
            } else if (this.converterClasses) {
                let items = this.converterClasses.filter(clazz => {
                    if (clazz.logicType === this.propertyType) {
                        return true;
                    }
                    if (clazz.logicType === 'java.lang.Object') {
                        return TypeKind.INTERFACE.equals(this.property.refKind)
                            || TypeKind.PLAIN.equals(this.property.refKind);
                    }
                    return false;
                }).map(clazz => {
                    return {
                        value: clazz.getName(),
                        text: clazz.simpleName,
                    };
                });
                if (items.length) {
                    items.unshift({
                        value: '',
                        text: '不使用转换器',
                    });
                }
                return items;
            }
            return null;
        },
    },
    watch: {
        modelValue() {
            this.model = this.modelValue;
        },
        model() {
            this.$emit('update:modelValue', this.model);
        },
    },
    mounted() {
        if (this.fixedAuto) {
            this.model = EntityPropertyMapping.CONVERTER_AUTO;
        } else {
            let version = MavenProjectUtil.getVersion(tnx.app.global.modelingProject.maven);
            truenewxLib.loadConverters(version.truenewx, lib => {
                this.converterClasses = lib.classes.map(clazz => clazz.origin);
                this.$nextTick(() => {
                    // 不可用时赋值为空
                    if (this.unavailable || TypeKind.ENUM.equals(this.property.refKind)) {
                        this.model = '';
                    }
                });
            });
        }
    },
    methods: {
        onDropdownCommand(value) {
            this.model = value;
        },
    }
}
</script>

<style lang="scss">
.converter-select-container > .el-badge {
    line-height: 14px;
}
</style>
